<?php
/**
 * Page administrator panel
 */
class Page_AdminController extends Zend_Controller_Action
{
    /**
     * Pages list
     * @return void
     */
    public function indexAction()
    {
        $modelPages = new Page_Model_Pages();
        $list = $modelPages->getList();
        $this->view->identity = Zend_Auth::getInstance()->getIdentity();
        $this->view->list = $list;
    }

    public function addAction() {
        $this->_forward('edit');
    }

    /**
     * Page edit form
     * @return void
     */
    public function editAction() {
        $form = new Page_Form_Edit();

        $pageId = $this->getRequest()->getParam('id');
        if(!empty($pageId)) {
            $pagesModel = new Page_Model_Pages();
            $pageMapper = $pagesModel->getPageById($pageId);
            $this->view->title = $this->view->translate('page') . ' &laquo' . $pageMapper->title .'&raquo;';
            $form->populate($pageMapper->toArray());
        } else {
            $pageMapper = new Page_Model_Mapper_Page();
            $this->view->title = $this->view->translate('new_page');
        }


        if ( $this->getRequest()->isPost() ) {
            if ( $form->isValid( $this->getRequest()->getPost() ) ) {
                $pageMapper->fromArray($form->getValues());

                if($pageMapper->exists() == false) {
                    $paramsForm = new Page_Form_ListParams();
                    $paramsForm->getElement('pagename')->setValue($pageMapper->sysname);

                    $dataToSend = array(
                        'action' => array(
                            'module' => 'page',
                            'controller'=>'index',
                            'action' => 'view'

                        ), //parent mca
                        'title' => $pageMapper->title,
                        'params' => array('pagename'=>$paramsForm->getValue('pagename'))
                    );

                    $apiRequest = new Slys_Api_Request($this, 'sysmap.create-extend', $dataToSend);
                    $apiRequest->proceed();
                }

                $pageMapper->save();

                $this->_helper->getHelper('FlashMessenger')->addMessage('save_ok');
                $this->_helper->redirector->goToRoute(array(
                                                            'module' => 'page',
                                                       ), 'admin', true);
            }
        }




        $this->view->form = $form;
    }

    /**
     * Page delete action
     * @return void
     */
    public function deleteAction()
    {
        $id = $this->getRequest()->getParam('id');
        $pageMapper = Page_Model_DbTable_Page::getInstance()->findOneBy('id',$id);
        $pageMapper->delete();

        $this->_helper->getHelper('FlashMessenger')->addMessage('save_ok');
        $this->_helper->redirector->goToRoute(array(
                                                    'module' => 'page',
                                               ), 'admin', true);
    }
}
